package com.bloveoa.zkkd.service.impl;

import com.bloveoa.zkkd.doman.LockTokenAward;
import com.bloveoa.zkkd.doman.LockedTokenModel;
import com.bloveoa.zkkd.doman.UserModel;
import com.bloveoa.zkkd.repository.BoxInfoRepository;
import com.bloveoa.zkkd.repository.UserRepository;
import com.bloveoa.zkkd.service.LockedTokenService;
import com.bloveoa.zkkd.service.UserService;
import com.bloveoa.zkkd.util.DateUtils;
import com.bloveoa.zkkd.util.NumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    private final UserRepository repository;
    private final BoxInfoRepository repository2;

    private final  LockedTokenService lockedTokenService;

    public UserServiceImpl(UserRepository repository, BoxInfoRepository repository2, LockedTokenService reLockedTokenService) {
        this.repository = repository;
        this.repository2 = repository2;
        this.lockedTokenService = reLockedTokenService;
    }
    public UserModel saveBoxUser(String address,String mac){
        address = address.toLowerCase();
        mac = mac.toLowerCase();
        int tokenNum  = 0;
        if(repository2.existsBoxInfoModelByTMac(mac)){
            tokenNum = repository2.queryByTMac(mac).getTokenNun();
        }
        if(repository.existsByAddress(address)){
            UserModel userModel = repository.queryByAddress(address).setBoxMac(mac).setTokenNum(tokenNum);
            return repository.save(userModel);
        }else{
            UserModel userModel = new UserModel().setAddress(address).setTokenNum(tokenNum).setBoxMac(mac);
            return repository.save(userModel);
        }
    }

    @Override
    public UserModel saveUser(UserModel userModel) {
        return repository.save(userModel);
    }

    @Override
    @Transactional
    public int updateLockNum(LockTokenAward lockTokenAward,LockedTokenModel lockedTokenModel) {
        double v =0;
        long days = DateUtils.getDays(lockedTokenModel.getCreateTime());
        if((days-1)/30>=lockedTokenModel.getLockedLong()){
            lockedTokenModel.setFlagDelete(true);
            lockedTokenService.saveLockedToken(lockedTokenModel);
            UserModel userModel = repository.queryById(NumberUtil.getIntByStr(lockedTokenModel.getUserId()));
            //获取锁仓收益数量
            switch (lockedTokenModel.getLockedLong()) {
                case 3:
                    v = lockedTokenModel.getLockedTokenNum() * NumberUtil.getDoubleByStr(lockTokenAward.getThreeAward());
                    break;
                case 6:
                    v = lockedTokenModel.getLockedTokenNum() * NumberUtil.getDoubleByStr(lockTokenAward.getSixAward());
                    break;
                case 12:
                    v = lockedTokenModel.getLockedTokenNum() * NumberUtil.getDoubleByStr(lockTokenAward.getYearAward());
                    break;
            }
            //数量=现在的数量+锁仓的数量+收益
            double v1 = userModel.getTokenNum() +lockedTokenModel.getLockedTokenNum()+ v;
            //设置现在的token数量
            userModel.setTokenNum((int) v1);
            //设置锁仓数量为现在的数量-到期的数量
            userModel.setLockedNum(userModel.getLockedNum()-lockedTokenModel.getLockedTokenNum());
            //返回收益的数量
            repository.save(userModel);
        }
        return (int) v;
    }

    public UserModel queryByAddress(String address){
        address = address.toLowerCase();
        if(repository.existsByAddress(address)){
            return  repository.queryByAddress(address);
        }else {
            UserModel userModel = new UserModel().setAddress(address);
            return repository.save(userModel);
        }
    }

    @Override
    public boolean existsByMac(String mac) {
        mac = mac.toLowerCase();
        return repository.existsByBoxMac(mac);
    }

    public boolean existsByAddress(String address){
        address = address.toLowerCase();
      return repository.existsByAddress(address);
    }
}
